package metier;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import DB.Request;
import bean.MessageTexte;

public class MessageTexteMetier {
	private static MessageTexteMetier singleton = new MessageTexteMetier();
	
	public static MessageTexteMetier getInstance(){
		return singleton;
	}
	
	public MessageTexte save(MessageTexte message_texte){
		if(message_texte != null){
			String query;
			if (message_texte.getId_message_texte() != null) {
				//UPDATE
				query = "UPDATE message_texte SET index_ligne=?,texte=?,id_message_entete=? WHERE id_message_texte="+message_texte.getId_message_texte();
			} else {
				//INSERT
				query = "INSERT INTO message_texte(index_ligne,texte,id_message_entete) VALUES (?,?,?)";
			}
			List<Object> param = new ArrayList<Object>();
			param.add(message_texte.getIndex_ligne());
			param.add(message_texte.getTexte());
			param.add(message_texte.getMessageEntete().getId_message_entete());
			Request.getInstance().executeUpdate(query, param);
		}
		if (message_texte.getId_message_texte() != null) {
			return getById(message_texte.getId_message_texte());
		} else {
			return getById(getLastId());
		}
	}
	
	public void delete(MessageTexte message_texte){
		String query = "DELETE FROM message_texte WHERE id_message_texte=" + message_texte.getId_message_texte();
		Request.getInstance().executeUpdate(query);
	}
	
	public MessageTexte getById(Integer id){
		MessageTexte message_texte = null;
		String query = "SELECT * FROM message_texte WHERE id_message_texte=" + id;
		ResultSet resultat = Request.getInstance().executeQuery(query);
		try {
			while(resultat.next()){
				message_texte = new MessageTexte(id, resultat.getString("index_ligne"), resultat.getString("texte"), MessageEnteteMetier.getInstance().getById(resultat.getInt("id_message_entete")));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return message_texte;
	}
	public MessageTexte getByIdEntete(Integer id){
		MessageTexte message_textes = null;
		String query = "SELECT * FROM message_texte WHERE id_message_entete=" + id;
		ResultSet resultat = Request.getInstance().executeQuery(query);
		try {
			while(resultat.next()){
				message_textes = new MessageTexte(resultat.getInt("Id_message_texte"), resultat.getString("index_ligne"), resultat.getString("texte"), MessageEnteteMetier.getInstance().getById(resultat.getInt("id_message_entete")));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return message_textes;
	}
	public List<MessageTexte> getAll(){
		List<MessageTexte> message_textes = new ArrayList<MessageTexte>();
		String query = "SELECT * FROM message_texte";
		ResultSet resultat = Request.getInstance().executeQuery(query);
		try {
			while(resultat.next()){
				MessageTexte message_texte = new MessageTexte(resultat.getInt("id_message_texte"), resultat.getString("index_ligne"), resultat.getString("texte"), MessageEnteteMetier.getInstance().getById(resultat.getInt("id_message_entete")));
				message_textes.add(message_texte);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return message_textes;
	}
	
	private Integer getLastId(){
		Integer result = null;
		String query = "SELECT max(id_message_texte) AS id_message_texte FROM message_texte";
		ResultSet resultat = Request.getInstance().executeQuery(query);
		try {
			while(resultat.next()){
				result = resultat.getInt("id_message_texte");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
}
